package com.juyu.miliao.scheduler.dao.redis;


import com.juyu.miliao.common.constant.OnlineUserRedis;
import com.juyu.miliao.common.constant.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

@Repository
public class LineTimeRedisDao {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 判断主播是否在线
     *
     * @param accid userId
     * @return boolean
     */
    public Boolean isMember(String accid) {
        return stringRedisTemplate.opsForSet().isMember(RedisKeys.SET_HOSTLINE.getValue(), accid);
    }

    /**
     * 存放在线用户id
     * @param accid
     * @return
     */
    public Boolean hasKeyLine(String accid) {
        return stringRedisTemplate.opsForHash().hasKey(RedisKeys.HASH_COUNTLINE.getValue(), accid);
    }

	public  Set<String> getOLineBySex(int sex,int page ,int pageSize) {
        Set<String> range = new HashSet<>();
        if (sex == 1) {
            //获取在线男用户
           range = stringRedisTemplate.opsForZSet().range(OnlineUserRedis.ZSET_BOY_ONLINE + 1, page, pageSize);
        } else if (sex == 2) {
            range = stringRedisTemplate.opsForZSet().range(OnlineUserRedis.ZSET_Girl_ONLINE + 2, page, pageSize);
        }
        return range;
    }

    /**
     * 存放正在通话中的主播id
     * @param userId
     * @return
     */
    public Boolean findHostCall(String userId){
        return stringRedisTemplate.opsForSet().isMember(RedisKeys.SET_HOSTCALL.getValue(), userId);
    }


    //1.5.6 获取主播每小时收益
    public double getHomeListIncomeByHostUserByUserId(long userId) {
        LocalDateTime dt = LocalDateTime.now();
        //homeListIncomeByHostUser:2019-10-24:13
        String time = dt.getYear()+"-"+dt.getMonthValue()+"-"+dt.getDayOfMonth()+":"+dt.getHour();
        Double score = stringRedisTemplate.opsForZSet().score("homeListIncomeByHostUser:"+time,userId+"");
        if(score==null){
            return 0;
        }
        return score;
    }

}
